﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UIDATALayer;

namespace UserData
{
    public partial class Control_ImageShow : UserControl, Interface_UserControl
    {
        private UIDATALayer.UIDataAction<ImageData> acImageData = null;

        string str_StationName = "";
        [Category("工站名称,所属工站名称包含此字符，即会在此工位显示图片")]
        public string 工站名称
        {
            set { str_StationName = value; }
            get { return str_StationName; }
        }
        public Control_ImageShow()
        {
            InitializeComponent();
        }
        public void Init()
        {
            ImageShow.OnTest_ShowImage += ShowImage;
            //Event_Log(工站名称, "软件加载成功");
        }

        public void ShowImage(string strName, Bitmap bitmap)
        {
            if (acImageData == null)
            {
                UIDATALayerHelper.RegistAction<ImageData>(nameof(ImageData), ShowImage).Start();
                acImageData = UIDATALayer.UIDATALayerHelper.GetRegistAction<ImageData>(nameof(ImageData));
            }
            if (strName == this.工站名称)
            {
                ImageData data = new ImageData();
                data.stationName = strName;
                data.value = bitmap;
                acImageData.SetData(data);

            }
        }

        public void ShowImage(ImageData data)
        {
            if (InvokeRequired)
            {
                this.Invoke(new Action(() => { ShowImage(data); }));
            }
            else
            {
                if (data.stationName == str_StationName)
                {
                    pic_Image.Image?.Dispose();
                    pic_Image.Image = (Bitmap)data.value.Clone();//解决切换界面时，刷新失败问题
                    data.value.Dispose();
                    GC.Collect();
                }
                  
            }
        }
    }
}
